<?php
namespace Tbf\Bundle\CronBundle\Entity;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\EntityRepository;

class CronRepository extends EntityRepository
{
    public function persist($cron)
    {
        if ($cron instanceof Cron) {
            $cron->onPrePersist();
        }
        $this->getEntityManager()->persist($cron);
        $this->getEntityManager()->flush();
    }
    public function remove(Cron $cron)
    {
        $this->getEntityManager()->remove($cron);
        $this->getEntityManager()->flush();
    }

    /**
     * @return Cron[]
     */
    public function getNeedRunList(\DateTime $util)
    {
        $where = new Criteria(Criteria::expr()->lte('next_time',$util));

        return $this->matching($where);
    }
    public function findPersistCronByName($name)
    {
        return $this->getEntityManager()->getRepository('TbfCronBundle:PersistCron')->find($name);
    }

    /**
     * @return \Doctrine\ORM\EntityManager
     */
    public function getEm(){
        return $this->_em;
    }
}
